<template>
  <van-skeleton v-if="newsInfo == null" title :row="5" />
  <div class="content" v-if="newsInfo != null" v-html="newsInfo.body"></div>
  <div class="nav-box">
    <a href="javascript:;"  class="back">
      <van-icon @click="back" name="arrow-left" />
    </a>
    <div class="small-box">
      <van-icon name="good-job-o" />
      <span>123</span>
    </div>
    <div class="small-box">
      <van-icon name="star-o" />
      <span>123</span>
    </div>
    <div class="small-box">
      <van-icon name="comment-o" />
      <span>123</span>
    </div>
    <div class="small-box">
      <van-icon name="share-o" />
      <span>123</span>
    </div>
    
  </div>
</template>

<script>
import { useRouter, useRoute } from "vue-router";
import { getNewsDetail } from "../api/index.js";
import {onBeforeUnmount, onMounted, reactive, toRefs, onBeforeMount, onUpdated } from "vue";

export default {
  setup(props) {
    const router = useRouter();
    let route = useRoute();

    let state = reactive({
      newsInfo: null,
    });
    onBeforeMount(() => {
      getNewsDetail(route.params).then((res) => {
        console.log(res);
        state.newsInfo = res;
        let link = document.createElement("link");
        link.id = "link";
        link.rel = "stylesheet";
        link.href = state.newsInfo.css[0];
        document.head.appendChild(link);
      });
    });
    onBeforeUnmount(() => {
        let link = document.getElementById("link");
        if(!link) return;
        document.head.removeChild(link);

    });
    onUpdated(() => {
      let imgPlaceholder = document.querySelector(".img-place-holder");
      if(imgPlaceholder){
        if(imgPlaceholder.innerHTML.trim() == ""){
        imgPlaceholder.innerHTML += `<img class="top" src= "${state.newsInfo.image}" alt="" />`
        }
      }
    })
    const back = () => {
        router.back()
    }

    return {
      ...toRefs(state),back
    };
  },
};
</script>

<style lang="less" scoped>
.van-skeleton {
  padding: 30px 15px;
}

.content {
  margin-top: -30px !important;
 
}



:deep(.headline) {
  overflow: hidden;
}
.nav-box {
  position: fixed;
  bottom: 0;
  height: 40px;
  width: 100vw;
  border-top: 1px solid black;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  background: #fff;

  .back {
    display: block;
    width: 15%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-start;
  }

  .small-box {
    width: 25%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;

    span {
      font-size: 10px;
    }
  }
}
</style>
